import { ccs } from "../../../util/services";
import { assetsType, cardType, companyType, figureType, itemType, playerType } from "../../gameType";
import figureModel from "../../model/figureModel";
import { game } from "../game";


export class ItemBase {
    count: number = 0;
    bagType: string;
    data: any = null;
    constructor(data, count: number = 1) {
        this.data = data;
        this.count = count;
        this.bagType = data.type;
    }
}
export class assetsBase extends ItemBase {

}
export class cardBase extends ItemBase {
    card: cardType = this.data;
    reduceTime() {
        this.card.leftTime --;
    }
}
export class figureBase extends ItemBase {
}
export class companyBase extends ItemBase {
    company: companyType = this.data;
    expandCompany() {
        let company = this.company;
        let cost = Math.trunc(Math.pow(company.levelCost, company.level));
        if(company.money >= cost) {
            company.maxWorker += Math.trunc(company.maxWorker * 0.2);
            company.money -= cost;
            company.rent += Math.trunc(company.rent * 0.2);
        }
    }
    //修改员工
    fixMaker(type: string, count: number ) {
        if(type == "make") {
            this.company.makeType.count = count;
        } else {
            this.company.salesType.count = count;
        }
        this.calEffect();
    }
    addWorker(count: number) {
        if(this.company.workers + count > this.company.maxWorker ) {
            ccs.winMgr.showWin("toast", {msg: "已满"});
            return ;
        }

        if(this.company.money < this.company.level * 10000) {
            ccs.winMgr.showWin("toast", {msg: "金钱不足"});
            return;
        }
        this.addPay(count * 200);
        this.company.workers += count;
        this.company.pay += count * this.company.wage;
    }
    //设置招聘人才列表
    addResume(hero: playerType) {
        if(!this.company.resumeList) {
            this.company.resumeList = [];
        }
        this.company.resumeList.push(hero);
    }
    //聘用ceo
    hireCeo(hero: playerType) {
        if(this.company.ceo) {
            ccs.winMgr.showWin("toast", { msg: "请先卸任当前的ceo"});
            return false;
        }else if(this.company.ceo && this.company.ceo.id === hero.id ) {
            ccs.winMgr.showWin("toast", { msg: "当前人物已在职"});
            return false;
        }
        this.company.ceo = hero;
        this.company.pay += hero.work.income;
        this.calEffect();
        return true;
    }
    unHireCeo() {
        this.company.pay -= this.company.ceo.work.income;
        this.company.money -= this.company.ceo.work.income;
        this.addPay(this.company.ceo.work.income); //支出增加
        this.company.ceo = null;
        this.calEffect();
    }
    addPay(money: number) {
        this.company.money -= money;
        this.company.payMonth += money;
    }
    addIncome(money: number) {
        this.company.money += money;
        this.company.incomeMonth += money;
        game.recordMgr.addList({action: `${this.company.name}当月收入${this.company.incomeMonth}, 支出：${this.company.payMonth}`});
        if(this.company.incomeMonth >= 100000) {
            game.playerMgr.achieveFinish('monthIncome', 1);
        }
    }
    delWorker(count: number) {
        if(this.company.workers - count < 0 ) {
            ccs.winMgr.showWin("toast", {msg: "员工都跑光了"});
            return ;
        }
        this.company.workers -= count;
        this.company.pay -= count * this.company.wage;
    }
    //计算月 数据
    calRent() {
        // 清空招聘列表
        if(this.company.resumeList && this.company.resumeList.length > 0 ) {
            this.company.resumeList = [];
        }
        this.company.payMonth = 0;
        this.company.incomeMonth = 0;
        this.addPay(this.company.rent); //租金
        this.addPay(this.company.pay); //工资
        this.calProductCount();
        //如果当前状态为负债，则累计 ，负债一年强制将强制破产
        if(this.company.money < 0) {
            this.company.debt --;
            if(this.company.debt == 0) {
                ccs.winMgr.showWin('toast', {msg: "公司濒临破产，请及时调整"});
            }else if(this.company.debt < 0) {
                this.broke();
            }
        } else {
            this.company.debt = 12;
        }
    }
    broke() {
        let money = 0;
        let msg = '';
        if(Math.abs(this.company.money) > this.company.inverst) {
            money = -this.company.inverst;
        } else {
            money = this.company.money;
        }
        msg = `公司经营不善破产，剩余欠款${ccs.gf.getOmitNum(Math.abs(money))}元`;
        game.playerMgr.heroInfo.money += money;
        game.playerMgr.bag.companyBag.delItem(this.company.id);
        ccs.mainEmitter.emit('upCompany');
        ccs.mainEmitter.emit('updateAttribute');
        this.company.isLock = 1;
        ccs.winMgr.showWin("toast", {
            msg: `${msg}`,
        });
        game.recordMgr.addList({action: msg});
    }
    calIncome(count: number) {
        //计算收入
        //生产效率为 满意度影响 满意度受ceo的智商和专业，销售情况视为魅力和情商影响
        this.addIncome(count * this.company.salesType.cost);
    }
    calProductPay(count: number) {
        this.addPay(count * this.company.makeType.cost);
    }
    calMake() {
        //计算生产力
        this.company.makeType.month = this.company.makeType.count * this.company.makeType.person * this.company.makeType.effect;
        return this.company.makeType.month; 
    }
    //计算每月的销售收入， 生产支出， 原材料
    calProductCount() {
        let effect = game.playerMgr.getCompanyEffect(this.company.skill);
        let sales = ccs.gf.randomInt(effect * this.company.salesType.month, this.company.salesType.month);
        let make = ccs.gf.randomInt(effect * this.company.makeType.month, this.company.makeType.month);
        //如果生产成本不够，只能产出最大的值
        if( this.company.money > 0) {
            if(make * this.company.makeType.cost > this.company.money) {
                make = this.company.money/this.company.makeType.cost;
            }
        } else {
            make = 0;
        }
        
        //原材料处理
        this.company.productCount += make;  
        this.calProductPay(make);
        
        if(this.company.productCount >= sales) {
            this.company.productCount -= sales;
            this.calIncome(sales);
        } else {
            let effect = game.playerMgr.getCompanyEffect(this.company.skill);
            this.calIncome(ccs.gf.randomInt(effect * this.company.productCount, this.company.productCount));
            this.company.productCount -= this.company.productCount;
        }
    }
    ceoUp() {
        //ceo成长
        if(!this.company.ceo || this.company.ceo.id == game.playerMgr.heroInfo.id) {
            return ;
        }
        let percent = this.company.ceo.learn/100000;
        this.company.ceo.iq += percent * this.company.ceo.iq;
        this.company.ceo.eq += percent * this.company.ceo.eq;
        this.company.ceo.charm += percent * this.company.ceo.charm * 0.5;
        this.company.ceo.age += 0.083;
        if(this.company.ceo.iq >= 300) {
            this.company.ceo.iq = 300;
        }
        if(this.company.ceo.eq >= 300) {
            this.company.ceo.eq = 300;
        }
        if(this.company.ceo.charm >= 200) {
            this.company.ceo.charm = 200;
        }
        let work = this.company.ceo.work;
        this.company.ceo.work.curExp += percent * (work.exp[work.level - 1]/12) * ((10 - work.level)/10);
        if(this.company.ceo.work.curExp >= work[work.level]) {
            this.company.ceo.work.level ++;
            if(this.company.ceo.work.level >= 10) {
                this.company.ceo.work.level = 9;
            }
            this.company.ceo.work.income = work.income * (1 + work.gains[work.level - 1]);
        }
        this.calEffect();
    }
    calSales() {
        this.company.salesType.month = this.company.salesType.count * this.company.salesType.person * this.company.salesType.effect;
        return this.company.salesType.month;
    }
    calEffect() {
        let work = game.playerMgr.heroInfo.works.find(val => val.id == this.company.skill);
        this.company.makeType.effect = ((game.playerMgr.heroInfo.iq/115 + work.level/10)/2);
        this.company.salesType.effect = ((game.playerMgr.heroInfo.charm/100 + game.playerMgr.heroInfo.eq/115)/2); //销售
        if(this.company.ceo) {
            this.company.makeType.effect += ((this.company.ceo.iq/200 + this.company.ceo.work.level/10)/2) * 0.3;
            this.company.salesType.effect += ((this.company.ceo.charm/100 + this.company.ceo.eq/200)/2) * 0.3; //销售
        }
    }
    calUnworkers() {
        return this.company.workers - this.company.makeType.count - this.company.salesType.count;
    }
    //花钱
    costMoney(cost: number) {
        if(cost > this.company.money) {
            return false;
        }
        this.company.money -= cost;
        this.company.payMonth += cost;
        ccs.mainEmitter.emit("upInfo");
        return true;
    }
    addMoney(cost: number) {
        this.company.money += cost;
        return true;
    }
}